/****************************************************************************
 *
 * Copyright (C) 2003-2008 Los Alamos National Security, LLC
 *                         Packet Analytics Corporation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License Version 2 as
 * published by the Free Software Foundation.  You may not use, modify or
 * distribute this program under any other version of the GNU General
 * Public License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 ****************************************************************************/
package nfse.query;

public class TimeRange implements Comparable<TimeRange> {
    private Time startTime = null;

    private Time endTime = null;

    public TimeRange(Time startTime) {
        this.startTime = startTime;
        this.endTime = startTime;
    }

    public TimeRange(Time startTime, Time endTime) {
        this.startTime = startTime;
        this.endTime = endTime;
    }

    public int compareTo(TimeRange tr) {
        if ((tr.getStartTime().compareTo(startTime) == 0) && (tr.getEndTime().compareTo(endTime) == 0))
            return 0;
        if (tr.getStartTime().compareTo(startTime) > 0)
            return -1;
        if (tr.getStartTime().compareTo(startTime) < 0)
            return 1;
        return -1;
    }

    public boolean isRange() {
        if (endTime.compareTo(startTime) == 0)
            return false;
        return true;
    }

    public void extend(TimeRange tr) {
        if (extendsEnd(tr))
            endTime = tr.getEndTime();
        if (extendsStart(tr))
            startTime = tr.startTime;
    }

    public boolean extendsEnd(TimeRange tr) {
        return false;
    }

    public boolean extendsStart(TimeRange tr) {
        return false;
    }

    public boolean contains(TimeRange tr) {
        if (endTime == null) {
            if ((!tr.isRange()) && (tr.getStartTime().compareTo(startTime) == 0))
                return true;
            return false;
        }
        if ((tr.getStartTime().compareTo(startTime) >= 0) && (tr.getEndTime().compareTo(endTime) <= 0))
            return true;
        return false;
    }

    public void setStartTime(Time startTime) {
        this.startTime = startTime;
        // if (endTime == null)
        // endTime = startTime;
    }

    public void setEndTime(Time endTime) {
        this.endTime = endTime;
    }

    public Time getStartTime() {
        return startTime;
    }

    public Time getEndTime() {
        return endTime;
    }

    public String toXML() {
        String temp = "<Time ";
        temp += "start=\"" + startTime + "\"";
        if (endTime.compareTo(startTime) != 0)
            temp += " end=\"" + endTime + "\"";
        temp += "/>";
        return temp;
    }
}